<?php

namespace app\admin\controller\activityone;

use app\common\controller\Backend;
use addons\shopro\traits\CouponSend;
use think\Collection;
use think\Db;
use addons\shopro\traits;
use app\admin\model\User as UserModel;
/**
 * 每月打卡记录管理
 *
 * @icon fa fa-circle-o
 */
class Useronlist extends Backend
{
    use CouponSend;
    /**
     * Useronlist模型对象
     * @var \app\admin\model\activityone\Useronlist
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\activityone\Useronlist;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法

        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['events', 'user', 'checkin'])
                ->where($where)
                ->where('is_submit',1)
                ->where('is_coupon',2)
                ->order($sort, $order)
                ->group('user_id')
                ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('user')->visible(['username', 'nickname']);
                $row->getRelation('events')->visible(['name']);
                $row->getRelation('checkin')->visible(['name']);
            }
            
//             $rows = Db::query('select t.*,f.nickname,f1.name as eventName,f2.name checninName, 
//             count(t.numb) as numbTotal from fa_event_checkin_user t
//             inner join fa_user f on f.id=t.user_id
//             inner join fa_events f1 on f1.id=t.event_id
//             inner join fa_event_checkin f2 on f2.id=t.checkin_id
//             where t.checkin_id=:checkinid
// group by user_id',['checkinid' =>$idsone]);
           
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }

        $ids = $this->request->param('ids');
        $this->assignconfig('event_id', $ids);
        return $this->view->fetch();
    }


    public function del($ids = null)
    {
        if (false === $this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ?: $this->request->post("ids");
        $event_user_team_ids = explode(',',$ids);
        $event_checkin_user_list = Db::name('event_checkin_user')->where('id','in',$event_user_team_ids)->select();
        foreach($event_checkin_user_list as $k => $v){
            $event = Db::name('event_checkin')->where('id',$v['checkin_id'])->find();
            $rows = (new UserModel())->where('id ='.$v['user_id'])->find();
            $res['is_coupon'] = 1;
            $update = Db::name('event_checkin_user')->where('checkin_id',$v['checkin_id'])->where('user_id',$v['user_id'])->update($res);
            if($update){
                $this->giveCouponsNohidden($rows,$event['giftCoupon']);
            }
           
        }
        $this->success();
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
